﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Security.Cryptography;

namespace Kernel.Encrypt
{
    /// <summary>
    /// 机器码类
    /// </summary>
    public class MachineCode
    {
        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public MachineCode() { }
        #endregion

        #region 获取CPU序列号
        /// <summary>
        /// 获取CPU序列号
        /// </summary>
        /// <returns></returns>
        public static string GetCpuCode()
        {
            try
            {
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                String strCpuID = null;
                foreach (ManagementObject mo in moc)
                {
                    strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                    break;
                }
                return strCpuID;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取硬盘序列号
        /// <summary>
        /// 获取硬盘序列号
        /// </summary>
        /// <returns></returns>
        public static string GetHardDiskCode()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
                String strHardDiskID = null;
                foreach (ManagementObject mo in searcher.Get())
                {
                    strHardDiskID = mo["SerialNumber"].ToString().Trim();
                    break;
                }
                return strHardDiskID;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取网卡MAC地址
        /// <summary>
        /// 获取网卡MAC地址
        /// </summary>
        /// <returns></returns>
        public static string GetNetworkCode()
        {
            try
            {
                string mac = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        mac = mo["MacAddress"].ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return mac;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取时间序列号
        /// <summary>
        /// 获取时间序列号
        /// </summary>
        /// <returns></returns>
        public static string GetTimeCode()
        {
            try
            {
                Int64 idnum = 0;
                idnum += (Convert.ToInt64(DateTime.Now.ToString("yyyy")) - 1900) * 365 * 24 * 60 * 60;
                idnum += Convert.ToInt64(DateTime.Now.ToString("MM")) * 30 * 24 * 60 * 60;
                idnum += Convert.ToInt64(DateTime.Now.ToString("dd")) * 24 * 60 * 60;
                idnum += Convert.ToInt64(DateTime.Now.ToString("HH")) * 60 * 60;
                idnum += Convert.ToInt64(DateTime.Now.ToString("mm")) * 60;
                idnum += Convert.ToInt64(DateTime.Now.ToString("ss"));
                return idnum.ToString() + DateTime.Now.ToString("ffff");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取时间序列号
        /// <summary>
        /// 获取时间序列号
        /// </summary>
        /// <param name="TimeString">指定时间</param>
        /// <returns></returns>
        public static string GetTimeCode(DateTime TimeString)
        {
            try
            {
                Int64 idnum = 0;
                idnum += (Convert.ToInt64(TimeString.ToString("yyyy")) - 1900) * 365 * 24 * 60 * 60;
                idnum += Convert.ToInt64(TimeString.ToString("MM")) * 30 * 24 * 60 * 60;
                idnum += Convert.ToInt64(TimeString.ToString("dd")) * 24 * 60 * 60;
                idnum += Convert.ToInt64(TimeString.ToString("HH")) * 60 * 60;
                idnum += Convert.ToInt64(TimeString.ToString("mm")) * 60;
                idnum += Convert.ToInt64(TimeString.ToString("ss"));
                return idnum.ToString() + TimeString.ToString("ffff");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
